//
//  ServicesManager.m
//  ServiceDebianManager
//
//  Created by Léo Lego on 09/05/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ServicesManager.h"
#import "Service.h"
#import "CommandThread.h"
#import "ServerController.h"
#import "ServiceController.h"
#import "DirectCommand.h"
//NSString * key = @"keyOfSDM";
@implementation ServicesManager
@synthesize activityBar;
@synthesize labelMain;


-(id) init{
	self = [super init];
	if(self){
		array = [[NSMutableArray alloc] init];
		sshThread = [[CommandThread alloc] initWithMother:self];
		pathPlist = [[NSString alloc] initWithString:@"/Users/Shared/.lsm.plist"];
		info = [[NSAlert alloc] init];
		serverController = [[ServerController alloc] initWithWindowNibName:@"ServerManager" AndMother:self];
		serviceController = [[ServiceController alloc] initWithWindowNibName:@"ServiceController" AndMother:self];
		directCommand = [[DirectCommand alloc] initWithWindowNibName:@"DirectCommand" AndMother:self];
		NSLog(@"Service manager loaded");
	}
	[self loadPlist];
	[NSThread detachNewThreadSelector:@selector(initThread) toTarget:self withObject:nil];
	return self;
}

/* tableview */
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
	return [array count]; 
}


-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
	Service *s = [array objectAtIndex:row]; 
	NSString *identifier = [tableColumn identifier];
	return [s valueForKey:identifier];
}

- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
	Service *s = [array objectAtIndex:row]; 
	NSString *identifier = [tableColumn identifier]; 
	[s setValue:object forKey:identifier];
	
}


/* action */

-(IBAction)start:(id)sender{ NSLog(@"Start");	
/*	for(Service *s in array){
		NSLog(@"%@",s);
	}*/
	NSInteger row = [tableview selectedRow];
	[tableview abortEditing];
	if(row != -1)
	{
		NSLog(@"%@",[array objectAtIndex:row]);
		[sshThread setAllCommand:[[array objectAtIndex:row] service] Value:@"start" Passwd:serverController.passwd Host:serverController.host Port:serverController.port];
		[NSThread detachNewThreadSelector:@selector(commandServices) toTarget:sshThread withObject:nil];
	} else {
		[info setMessageText:@"No service selected !"];
		[info runModal];
	}

	
}
-(IBAction)stop:(id)sender{	
	NSInteger row = [tableview selectedRow];
	[tableview abortEditing];
	if(row != -1)
	{
		NSLog(@"%@",[array objectAtIndex:row]);
		[sshThread setAllCommand:[[array objectAtIndex:row] service] Value:@"stop" Passwd:serverController.passwd Host:serverController.host Port:serverController.port];
		[NSThread detachNewThreadSelector:@selector(commandServices) toTarget:sshThread withObject:nil];
	} else {
		[info setMessageText:@"No service selected !"];
		[info runModal];
	}
}

-(IBAction)halt:(id)sender{ NSLog(@"halt");
	[sshThread setAllPasswd:serverController.passwd Host:serverController.host Port:serverController.port Command:@"halt"];
	[NSThread detachNewThreadSelector:@selector(commandSystem) toTarget:sshThread withObject:nil];
}
-(IBAction)reboot:(id)sender{ NSLog(@"Reboot");
	[sshThread setAllPasswd:serverController.passwd Host:serverController.host Port:serverController.port Command:@"reboot"];
	[NSThread detachNewThreadSelector:@selector(commandSystem) toTarget:sshThread withObject:nil];
}

/* menu bar show */
- (IBAction)showServerPreferences:(id)sender{
	NSLog(@"Settings for serveur");
	[serverController showWindow:self];
	if(![[serverController panel] isVisible]){
		[[serverController panel] setIsVisible:YES];
	} 
}

/* services settings */

-(IBAction)removeServiceButton:(id)sender{ NSLog(@"removeService");
	NSInteger row = [tableview selectedRow];
	[tableview abortEditing];
	if(row != -1)
		[array removeObjectAtIndex:row];
	[tableview reloadData];	
	[self savePlist];
}

- (IBAction)addServiceButton:(id)sender { NSLog(@"AddServiceButton");
	[serviceController showWindow:self];
	if(![[serviceController panel] isVisible]){
		[[serviceController panel] setIsVisible:YES];
	} 
}

-(void)addServiceWithService:(NSString *)nService{NSLog(@"Add service to array");
	[array addObject:[[Service alloc] initWithService:nService]];
	[tableview reloadData];
	[self savePlist];
}
						   
/* full command */
-(void)fullCommandWithCommand:(NSString*)nCommand{
	[sshThread setAllPasswd:serverController.passwd Host:serverController.host Port:serverController.port Command:nCommand];
	[NSThread detachNewThreadSelector:@selector(commandSimple) toTarget:sshThread withObject:nil];
}

- (IBAction)fullCommandbutton:(id)sender {
	NSLog(@"direct command buuton");
	[directCommand showWindow:self];
	if(![[directCommand panel] isVisible]){
		[[directCommand panel] setIsVisible:YES];
	} 
}

/** PLIST */
- (void)savePlist{
	
	NSLog(@"writing");
	NSMutableArray *saveArray = [[NSMutableArray alloc] init];
	for(Service *s in array){
		[saveArray addObject:[s service]];
	}
	
	NSDictionary *plist = [NSDictionary dictionaryWithObjectsAndKeys:[serverController host],@"HOST",[serverController passwd],@"PASSWD",[serverController port],@"PORT",saveArray,@"Services", nil];
	[plist writeToFile:pathPlist atomically:YES];
	labelMain.stringValue = serverController.host;
}

- (void)loadPlist{
	NSDictionary * plist = [NSDictionary dictionaryWithContentsOfFile:pathPlist];
	if(plist){
		NSLog(@"%@",plist);
		[serverController loadWithHost:[plist valueForKey:@"HOST"] Pass:[plist valueForKey:@"PASSWD"] Port:[plist valueForKey:@"PORT"]];
		
		for(NSString * s in [plist valueForKey:@"Services"]){
			[self addServiceWithService:s];
		}
	} else {
		NSLog(@"echec read");
	}
	
}

/* thread */
- (void) initThread{
	[NSThread sleepForTimeInterval:0.25];
	labelMain.stringValue = serverController.host;
	[self unactiveBar];
}
/* active bar */
- (void) activeBar{NSLog(@"activeBar");
	[activityBar startAnimation:self];
		[activityBar setHidden:NO];
}
- (void) unactiveBar{NSLog(@"unactiveBar");
	[activityBar stopAnimation:self];
	[activityBar setHidden:YES];
}

- (BOOL) isNotActive{
	return [activityBar isHidden];
}



@end
